package neko.net;
import neko.net.UdpReliableSocket;
import neko.io.Error;
import neko.net.Socket;

class UdpReliableSocketOutput
	extends neko.io.Output,
	implements neko.net.SocketOutput
{
	private var __socket 	: UdpReliableSocket;
	var __s 				: SocketHandle; // not used.

	public function new(s) {
		__socket = s;
	}

	public override function writeChar( c : Int ) {
		try {
			udpr_send_char(untyped __socket.__h, untyped __socket.__p, c, 0, true);
		} catch( e : Dynamic ) {
			throw Custom(e);
		}
	}

	public override function writeBytes( buf : String, pos : Int, len : Int) : Int {
		return try {
			udpr_send(untyped __socket.__h, untyped __socket.__p, untyped buf.__s, pos, len, 0, true);
		} catch( e : Dynamic ) {
			throw Custom(e);
		}
	}

	public override function close() {
		super.close();
		if( __socket != null ) __socket.close();
	}

	private static var udpr_send = neko.Lib.load("udprsocket", "udpr_send", 6);
	private static var udpr_send_char = neko.Lib.load("udprsocket", "udpr_send_char", 4);
}
